package com.raft.core.node.role;

import com.raft.core.node.NodeId;

public abstract class AbstractNodeRole {

    private final RoleName name; // 当前角色名称
    protected final int term; // term

    AbstractNodeRole(RoleName name, int term) {
        this.name = name;
        this.term = term;
    }

    public RoleName getName() {
        return name;
    }

    public int getTerm() {
        return term;
    }

    public RoleNameAndLeaderId getNameAndLeaderId(NodeId selfId) {
        return new RoleNameAndLeaderId(name, getLeaderId(selfId));
    }

    public abstract NodeId getLeaderId(NodeId selfId);

    /**取消超时或定时任务，用于取消每个角色对应的选举超时或者日志复制定时任务。
     * <br/>实际代码中，从一个角色到另一个角色时，必须取消当前的超时或者定时任务，然后创建新的超时或者定时任务
     */
    public abstract void cancelTimeoutOrTask();

    public abstract RoleState getState();

    public boolean stateEquals(AbstractNodeRole that) {
        if (this.name != that.name || this.term != that.term) {
            return false;
        }
        return doStateEquals(that);
    }

    protected abstract boolean doStateEquals(AbstractNodeRole role);

}
